package com.landimc.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.landimc.sms.constants.AliYunSmsProperty;
import com.landimc.sms.entity.AliYunSms;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 短信发送辅助类
 *
 * @author Yang
 * @version 1.0
 * @date 2020/10/26 12:26
 */
@Slf4j
@Component
public class AliYunSmsHelper {

    /**
     * 短信发送辅助类
     *
     * @param aliYunSms
     * @return
     * @throws ClientException
     */
    public String sendMessage(AliYunSms aliYunSms) throws ClientException {
        log.info("access_key: {}", AliYunSmsProperty.ACCESS_KEY);
        log.info("access_secret: {}", AliYunSmsProperty.ACCESS_SECRET);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AliYunSmsProperty.ACCESS_KEY, AliYunSmsProperty.ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", aliYunSms.getPhone()[0]);
        request.putQueryParameter("SignName", aliYunSms.getSingName());
        request.putQueryParameter("TemplateCode", aliYunSms.getTemplateCode());
        request.putQueryParameter("TemplateParam", aliYunSms.getParameters().toString());
        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }

}
